package Function.monsterattack;

import FCRendering.FCRBattleOrderConstant;
import FCRendering.azer.PlayerAction;
import Function.FCMonsterBattleOrderHelper;
import Function.FCMutilPlayCache;
import VWorldGameStudio.FCBattleOrder;
import VWorldGameStudio.FCMonster;
import VWorldGameStudio.FCPlayerBattleWrap;
import VWorldGameStudio.FCPlayerInfo;

import java.util.ArrayList;

//特定的怪物创建指令策略
public class Dog extends AMonsterFakeAI{
    @Override
    public FCBattleOrder createMonsterOrder(FCMonster monster, int num) {
        FCBattleOrder ordert = new FCBattleOrder();
        ArrayList<FCPlayerBattleWrap> wraps = FCPlayerBattleWrap.findAllWrapByHostId("npc");
        FCPlayerBattleWrap wrapt = null;
        if(wraps.size() > 0){//指定有npc就只打npc了
            FCPlayerInfo npc = FCMutilPlayCache.findPlayerByName(wraps.get(0).getHostUid(), wraps.get(0).getName());
            if(!npc.isDeath()){
                wrapt = wraps.get(0);
            }
        }
        if(wrapt == null){
            wrapt = FCMonsterBattleOrderHelper.findTarget(ordert.randomV4);
        }
        ordert.playerUid = wrapt.getHostUid();
        ordert.playerName = wrapt.getName();
        ordert.battleType = 2;
        ordert.monsterNum = num;
        int speed = monster.getLongTermVal(FCRBattleOrderConstant.S_AddSpeed);
        speed = speed == -1 ? 0 : speed;
        ordert.battleSpeed = monster.getSpeed() + speed;
        //TODO 选中攻击方式，4种，普通，主炮，副炮, 战斗道具
        ArrayList<Integer> attackTypes = new ArrayList<>(0);
        int allWeight = 0;
        if (monster.getGun1() != null) {
            attackTypes.add(1);
            allWeight += monster.getGun1().getAttackWeight();
        }
        if (monster.getGun2() != null) {
            attackTypes.add(2);
            allWeight += monster.getGun2().getAttackWeight();
        }
        if (monster.getGun3() != null) {
            attackTypes.add(3);
            allWeight += monster.getGun3().getAttackWeight();
        }
        if (monster.getKeepFcItems().size() > 0) {
            attackTypes.add(4);
            allWeight += monster.getUseItemsWeight();
        }
        int randWeight = (int)(ordert.randomV1*allWeight);
        //根据权重来选技能攻击
        int newAll = 0;
        for(Integer i: attackTypes){
            if(i == 1){
                newAll += monster.getGun1().getAttackWeight();
            }else if(i == 2){
                newAll += monster.getGun2().getAttackWeight();
            }else if(i == 3){
                newAll += monster.getGun3().getAttackWeight();
            }else if(i == 4){
                newAll += monster.getUseItemsWeight();
            }

            if(randWeight <= newAll){
                ordert.monsterAttackType = i;
                break;
            }
        }
        if (ordert.monsterAttackType == 4) {
            ordert.monsterUseItemIndex = Math.min(monster.getKeepFcItems().size() - 1, (int) (ordert.randomV4 * monster.getKeepFcItems().size()));
        }
        return ordert;
    }
}
